<?php

namespace App\Services;

use App\Models\SmsLog;
use Illuminate\Support\Facades\Log;
use Overtrue\EasySms\EasySms;
use Overtrue\EasySms\Exceptions\Exception;

class SmsService
{

    public static $config = [
        // HTTP 请求的超时时间（秒）
        'timeout' => 5.0,

        // 默认发送配置
        'default' => [
            // 网关调用策略，默认：顺序调用
            'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,

            // 默认可用的发送网关
            'gateways' => [
                // 'aliyun',
                'yuntongxun'
            ],
        ],
        // 可用的网关配置
        'gateways' => [
            'errorlog' => [
                'file' => '/tmp/easy-sms.log',
            ],
            // 			'aliyun' => [
            // 				'access_key_id' => 'LTAI5tGn9FJ4LYAzL3BaftP4',
            // 				'access_key_secret' => 'aS5AJU6ZZeGVq0el3ITjeFUX9U9g8P',
            // 				'sign_name' => '凯斯通',
            // 			],
            'yuntongxun' => [
                'app_id'         => '2c94811c8cd4da0a018fc1f838fc28ba', // 外讲库ID
                'account_sid'    => '8aaf0708591604f301591742cc90010f',
                'account_token'  => 'd925c1d7247144cfb0e16c174f10d618',
                'is_sub_account' => false,
            ]
            //...
        ],
        'SMS' => [
            'AUDIT_SUCCESS' => '2591309', // 审核通过发送短信
            'AUDIT_FAIL' => '2591375', // 审核失败发送短信
            'TOPAY_SUCCESS' => '2592755', // 支付成功
            'TOPAY_FAIL' => '2592756', // 支付失败
        ],
    ];




    public static function  sendCode($phone, $code, $template)
    {

        $easySms = new EasySms(self::$config);
        try {
            SmsLog::create(
                [
                    'phone' => $phone,
                    'code'    => json_encode($code),
                    'add_ip' => request()->ip(),
                    'status'    => 0,
                    'add_time'    => date('Y-m-d H:i:s', time()),
                ]
            );

            $res = $easySms->send($phone, [
                // 'content'  => '您的验证码为: 6379',
                'template' => self::$config['SMS'][$template],
                'data' => $code,
            ]);

            return $res;
        } catch (\Exception $e) {
            Log::error(' SmsService ERROR  ' . $e);
            // dd($e);
            return [];
        }
    }
}
